package com.java.zhuiyun.api.carousel.service.impl;

import com.java.zhuiyun.api.carousel.entity.Carousel;
import com.java.zhuiyun.api.carousel.mapper.CarouselMapper;
import com.java.zhuiyun.api.carousel.service.CarouselService;
import com.java.zhuiyun.api.front_desk_user.entity.FrontDeskUser;
import com.java.zhuiyun.common.response.CommonResult;
import com.java.zhuiyun.util.UploadFileUtils;
import com.java.zhuiyun.util.pageUtil.MybatisPageHelper;
import com.java.zhuiyun.util.pageUtil.PageRequest;
import com.java.zhuiyun.util.pageUtil.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @ClassName CarouselServiceImpl
 * @Description TODO
 * @Author wuwan
 * @Date 2023/8/15 16:19
 **/
@Service
public class CarouselServiceImpl implements CarouselService {

    @Autowired
    private CarouselMapper carouselMapper;

    /*
    * @Author wuwan
    * @Description todo 轮播图数据查询
    * @Date 2023/8/15 16:30
    * @Return
    * @param null
    */
    @Override
    public PageResult getCarousel(PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        PageResult pageResult = MybatisPageHelper.getPageResult(carouselMapper.getCarousel(pageRequest));
        return pageResult;
    }

    /*
    * @Author wuwan
    * @Description todo 轮播图添加
    * @Date 2023/8/15 21:03
    * @Return
    * @param null
    */
    @Override
    @Transactional
    public CommonResult addCarousel(Carousel carousel, MultipartFile file) {
        String s = UploadFileUtils.uploadFile(file);
        if (s ==""){
            return CommonResult.error("图片上传失败");
        }
        carousel.setImg(s);
        carousel.setCreateTime(new Date());
        carousel.setUpdateTime(new Date());
        carousel.setCarouselStatus(0);
        Integer integer = carouselMapper.addCarousel(carousel);
        if (integer<1){
            return CommonResult.error("添加失败");
        }
        return CommonResult.ok("上传成功");
    }

    /*
     * @Author wuwan
     * @Description todo 轮播图修改
     * @Date 2023/8/15 21:03
     * @Return
     * @param null
     */
    @Override
    @Transactional
    public CommonResult updateCarousel(Carousel carousel, MultipartFile file) {
        if (file == null){
            carousel.setUpdateTime(new Date());
            Integer integer = carouselMapper.updateData(carousel);
            if (integer<1){
                return CommonResult.error("修改失败");
            }
            return CommonResult.ok("上传成功");
        }else {
            String s = UploadFileUtils.uploadFile(file);
            if (s ==""){
                return CommonResult.error("图片上传失败");
            }
            carousel.setImg(s);
            carousel.setUpdateTime(new Date());
            Integer integer = carouselMapper.updateCarousel(carousel);
            if (integer<1){
                return CommonResult.error("修改失败");
            }
          return CommonResult.ok("上传成功");
        }
    }

    /*
     * @Author wuwan
     * @Description todo 轮播图启用禁用方法
     * @Date 2023/8/16 16:05
     * @Return
     * @param null
     */
    @Override
    public CommonResult ReleaseCarousel(Integer id,Integer carouselStatus) {
        Integer releasecarousel = carouselMapper.ReleaseCarousel(id,carouselStatus);
        if (releasecarousel >= 1){
            return CommonResult.ok("启用禁用成功");
        }
        return CommonResult.error("启用禁用失败");
    }
    /*
     * @Author wuwan
     * @Description todo 轮播图删除方法
     * @Date 2023/8/16 16:05
     * @Return
     * @param null
     */
    @Override
    public CommonResult deleteData(Integer id) {
        Integer deletedata = carouselMapper.deleteData(id);
        if (deletedata>=1){
            return CommonResult.ok("删除成功");
        }
        return CommonResult.error("删除失败");
    }


}
